package ru.linnenson.darshana.infrastructure.config;

import lombok.RequiredArgsConstructor;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@RequiredArgsConstructor
public class TesseractConfiguration {

  private final ApplicationProperties applicationProperties;

  @Bean
  public Tesseract tesseract() {

    final Tesseract tesseract = new Tesseract();
    final ApplicationProperties.OcrProperties ocrProperties = applicationProperties.getOcrProperties();

    tesseract.setDatapath(new ClassPathResource("tessdata").getPath());
    tesseract.setLanguage(ocrProperties.getLanguage());
    tesseract.setPageSegMode(ocrProperties.getPageSegMode());
    tesseract.setOcrEngineMode(ocrProperties.getEngineMode());

    return tesseract;
  }
}
